package com.example.mvp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

//import com.example.mvp.binder.BaseAdapterDataBinder;
import com.example.mvp.binder.BaseDataBinder;
import com.example.mvp.delegate.BaseAdapterDelegate;

import java.util.ArrayList;
import java.util.List;


/**
 * MVP模式的公共适配器基类（多Item模式）
 * Created by sun.li on 2018/6/6.
 *
 * @author sun.li
 */

public abstract class BaseLibMultipleViewAdapter<D extends Object> extends android.widget.BaseAdapter {

    private List<D> data;

    private Context context;

    public BaseLibMultipleViewAdapter(Context context, List<D> data) {
        this.context = context;
        if (null != data) {
            this.data = data;
        } else {
            this.data = new ArrayList<>();
        }
    }

    @Override
    public int getCount() {
        if (null == data) {
            return 0;
        }
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        if (null == data) {
            return null;
        }
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BaseAdapterDelegate viewDelegate = null;
        if (null == convertView) {
            viewDelegate = initDelegate(context,position);
            if (null != viewDelegate) {
                convertView = viewDelegate.getRootView();
                convertView.setTag(viewDelegate);
            }
        }else{
            viewDelegate = (BaseAdapterDelegate) convertView.getTag();
            if(viewDelegate.getClass() !=  initDelegate(context,position).getClass()){
                viewDelegate = initDelegate(context,position);
                if (null != viewDelegate) {
                    convertView = viewDelegate.getRootView();
                    convertView.setTag(viewDelegate);
                }
            }
        }
        try {
            if (null != getDataBinder(position) && null != getItemData(position) && getItemData(position).getClass() == getDataBinder(position).getDataClass()) {
                getDataBinder(position).viewBindModel(viewDelegate, getItemData(position));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        notifyView(viewDelegate, position);
        bindEvenListener(viewDelegate, position);
        return convertView;
    }

    /** 注册界面控件点击事件*/
    protected abstract void bindEvenListener(BaseAdapterDelegate viewDelegate, int position);

    /** 自定义实现View刷新操作，通常用于单个的刷新或者未进行数据绑定时使用，如已经进行过数据绑定，则无需具体实现此方法*/
    protected abstract void notifyView(BaseAdapterDelegate viewDelegate, int position);

//    /** 指定界面的视图控制器，必须指定*/
//    public abstract Class<T> getDelegateClass(int position);

    /** 指定界面的视图控制器，必须指定*/
    public abstract BaseAdapterDelegate getDelegate(int position);

    /** 公共数据视图绑定控制器数据对象*/
    public abstract BaseDataBinder getDataBinder(int position);

    public abstract Object getItemData(int position);

    public List<D> getData() {
        return data;
    }

    public void setData(List<D> data) {
        this.data = data;
    }

    public Context getContext() {
        return context;
    }

    /** 初始化视图托管类对象*/
    public BaseAdapterDelegate initDelegate(Context context,int position){
        BaseAdapterDelegate viewDelegate = getDelegate(position);
        if(null!=context){
            viewDelegate.create(LayoutInflater.from(context),null,null);
        }
//        if(null == binder){
//            binder = getDataBinder();
//        }
        return viewDelegate;
    }

//    @Override
//    public void notifyDataSetChanged() {
//        super.notifyDataSetChanged();
//    }


}
